package org.hxzon.rpggame.bean;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.hxzon.util.Dt;

//属性组
public class Attrs {

    private Map<String, Map<String, Long>> attrGroups;
    private Set<String> disableGroups;

    public Attrs() {
        attrGroups = new ConcurrentHashMap<String, Map<String, Long>>();
        disableGroups = new HashSet<String>();
    }

    //
    public void addAttrGroup(String groupName, Map<String, Long> attrs) {
        attrGroups.put(groupName, attrs);
    }

    public void addAttrs(String groupName, Map<String, Long> attrs) {
        Map<String, Long> attrsOrig = attrGroups.get(groupName);
        if (attrsOrig == null) {
            addAttrGroup(groupName, attrs);
        } else {
            mergeAttrs(attrsOrig, attrs);
        }
    }

    public void addAttr(String groupName, String attrName, long valueAdd) {
        Map<String, Long> attrsOrig = attrGroups.get(groupName);
        if (attrsOrig == null) {
            attrsOrig = new HashMap<String, Long>();
            attrGroups.put(groupName, attrsOrig);
        }
        mergeAttr(attrsOrig, attrName, valueAdd);
    }

    //
    public long getAttr(String groupName, String attrName) {
        Map<String, Long> attrsOrig = attrGroups.get(groupName);
        if (attrsOrig == null) {
            return 0;
        }
        return Dt.toLong(attrsOrig.get(attrName), 0);
    }

    public long getAttr(String attrName) {
        long total = 0;
        for (Map.Entry<String, Map<String, Long>> groupEntry : attrGroups.entrySet()) {
            if (disableGroups.contains(groupEntry.getKey())) {
                continue;
            }
            total += Dt.toLong(groupEntry.getValue().get(attrName), 0);
        }
        return total;
    }

    //
    public Map<String, Long> getAttrs(String groupName) {
        return attrGroups.get(groupName);
    }

    public Map<String, Long> getAttrs() {
        Map<String, Long> totals = new HashMap<String, Long>();
        for (Map.Entry<String, Map<String, Long>> groupEntry : attrGroups.entrySet()) {
            if (disableGroups.contains(groupEntry.getKey())) {
                continue;
            }
            mergeAttrs(totals, groupEntry.getValue());
        }
        return totals;
    }

    public Map<String, Long> getAttrs(String... groupNames) {
        Map<String, Long> totals = new HashMap<String, Long>();
        for (String groupName : groupNames) {
            Map<String, Long> attrs = getAttrs(groupName);
            if (attrs != null) {
                mergeAttrs(totals, attrs);
            }
        }
        return totals;
    }

    //
    public void enableGroup(String groupName) {
        disableGroups.remove(groupName);
    }

    public void disableGroup(String groupName) {
        disableGroups.add(groupName);
    }

    //
    public static void mergeAttrs(Map<String, Long> attrsOrig, Map<String, Long> attrs) {
        for (Map.Entry<String, Long> attr : attrs.entrySet()) {
            String attrName = attr.getKey();
            long valueAdd = Dt.toLong(attrs.get(attrName), 0);
            mergeAttr(attrsOrig, attrName, valueAdd);
        }
    }

    public static void mergeAttr(Map<String, Long> attrsOrig, String attrName, long valueAdd) {
        long valueOrig = Dt.toLong(attrsOrig.get(attrName), 0);
        attrsOrig.put(attrName, valueOrig + valueAdd);
    }
}
